package com.hnust.controller;

import com.hnust.dao.QuestionDao;
import com.hnust.domain.record.*;
import com.hnust.domain.request.AddRecordReqBody;
import com.hnust.domain.request.GetQuestionReqBody;
import com.hnust.service.ExerciseService;
import com.hnust.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping
public class ExerciseController {

    @Autowired
    private ExerciseService exerciseService;
    @Autowired
    private QuestionDao questionDao;


    @GetMapping("/getCollege")//浏览器输入：http://localhost:8080/api/student/getMajor?collegeId=123
    public Result<List<CollegeRecord>> getCollege(){
        List<CollegeRecord> collegeRecord = exerciseService.getCollege();
        if(collegeRecord.size()==0)
        {
            return new Result<>(0,"获取学院失败",collegeRecord);
        }else {
            return new Result<>(1,"获取学院成功",collegeRecord);
        }
    }
    @GetMapping("/getCourse")
    public Result<List<CourseRecord>> getCourse(@RequestParam(value="colId",required=false) String collegeId){
        List<CourseRecord> courseRecord = exerciseService.getCourse(collegeId);
        if(courseRecord.size()==0){
            return new Result<>(0,"获取学院课程失败",courseRecord);
        }else{
            return new Result<>(1,"获取学院课程成功",courseRecord);
        }

    }
    @GetMapping("/getChapter")
    public Result<List<ChapterRecord>> getChapter(@RequestParam(value="courseId",required=false) String courseId,
                                                  @RequestParam(value="level",required=false) Integer level
                                                  ){
        List<ChapterRecord> chapterRecord = exerciseService.getChapter(courseId,level);
        if(chapterRecord.size()==0){
            return new Result<>(0,"无章节",chapterRecord);
        }else{
            return new Result<>(1,"获取章节成功",chapterRecord);
        }

    }
    @GetMapping("/getQueType")
    public Result<List<QuestionTypeRecord>> getQuestionType(){
        List<QuestionTypeRecord> questionTypeRecord = exerciseService.getQuestionType();
        if(questionTypeRecord.size()==0){
            return new Result<>(0,"获取题目类型失败",questionTypeRecord);
        }else
        {
            return new Result<>(1,"获取题目类型成功",questionTypeRecord);
        }

    }
    @PostMapping("/paper")
    public Result<List<NewQuestionRecordRecord>> getQuestion(@RequestBody GetQuestionReqBody reqBody){

        List<NewQuestionRecordRecord> questionRecordRecord = exerciseService.getQuestion(reqBody.getTypeInfoList(),reqBody.getCapterIDList(),reqBody.getLevel());
        if(questionRecordRecord.size()==0){
           return new Result<>(0,"获取试卷失败",questionRecordRecord);
       }else{
           return new Result<>(1,"获取试卷成功", questionRecordRecord);
       }
    }

    @PostMapping("/addRecord")
    public Result<String> addRecord(@RequestBody AddRecordReqBody reqBody){
            String  msg = exerciseService.addRecord(reqBody.getQuestionIdList(),reqBody.getCreateTime(),reqBody.getStudentId(),reqBody.getQueRecType(),reqBody.getToken(),reqBody.getRight());
        if("添加记录成功".equals(msg)){
            return new Result<>(1,"添加记录成功", null);
        }else {
            return new Result<>(0,msg, null);
        }
    }


    @GetMapping("/getQuestions")
    public Result<QuestionSplitPageRecord> getQuestionsByCourseId(@RequestParam("courseId")String courseId, @RequestParam("page")Integer page, @RequestParam("chapterId")String chapterId){
        //一页20条
        //创建空返回结果
        QuestionSplitPageRecord result = new QuestionSplitPageRecord();
        //获取题目列表和总题目量
        List<NewQuestionRecordRecord> questionRecordRecord = exerciseService.getQuestionsByCourseIdAndPage(courseId,page,chapterId);
        if(chapterId.isEmpty()){
            chapterId=null;
        }
        int questionCount = questionDao.getQuestionsCountByCourseId(courseId,chapterId);
        result.setQuestionList(questionRecordRecord);
        result.setPages((int)Math.ceil((double)questionCount/20));
        if(questionCount==0){
            return new Result<>(0,"获取题目失败",result);
        }else{
            return new Result<>(1,"获取题目成功", result);
        }
    }

    @GetMapping("/getChapterTree")
    public Result<List<ChapterTreeItemRecord>> getChapterTree(@RequestParam("courseId") String courseId){
        List<ChapterTreeItemRecord> tree = null;
        String msg = "获取章节树目录成功";
        try{
            tree = exerciseService.getChapterTree(courseId);
        }catch(Exception e){
            msg = e.getMessage();
            e.printStackTrace();
        }
        return new Result<>(0,msg,tree);
    }
}
